<?
class Mapa {

	private	$areas = array();
	private	$data = array();

	function __construct ($nome, $descricao) {
		$this->data['nome']			= $nome;
		$this->data['descricao']	= $descricao;
	}
	
	private function coordsToArray($x, $y) {
		$array['x'] = $x;
		$array['y'] = $y;
		return $array;
	}
	
	public function setArea ($area, $xCoord, $yCoord) {
		$area->setCoords($xCoord, $yCoord);
		$this->areas[$xCoord][$yCoord] = $area->getValue('nome');
	}
	
	public function getArea ($CoordsArray, $manager) {
		if ($this->existeArea($CoordsArray)) 
			return $manager->readObj($this->areas[$CoordsArray['x']][$CoordsArray['y']], 'areas');
		else return false;
	}
	
	public function existeArea ($CoordsArray) {
		if ($this->areas[$CoordsArray['x']][$CoordsArray['y']] != null)
			return true;
		else return false;
	}
	
	public function toHtml ($coordsArray, $html, $manager) {
		$this->data['area_actual_div'] = $this->areaToHtml($coordsArray, 'actual', $html, $manager);
		$this->data['area_norte_div'] = $this->areaToHtml($coordsArray, 'norte', $html, $manager);
		$this->data['area_nordeste_div'] = $this->areaToHtml($coordsArray, 'nordeste', $html, $manager);
		$this->data['area_noroeste_div'] = $this->areaToHtml($coordsArray, 'noroeste', $html, $manager);
		$this->data['area_oeste_div'] = $this->areaToHtml($coordsArray, 'oeste', $html, $manager);
		$this->data['area_este_div'] = $this->areaToHtml($coordsArray, 'este', $html, $manager);
		$this->data['area_sudoeste_div'] = $this->areaToHtml($coordsArray, 'sudoeste', $html, $manager);
		$this->data['area_sudeste_div'] = $this->areaToHtml($coordsArray, 'sudeste', $html, $manager);
		$this->data['area_sul_div'] = $this->areaToHtml($coordsArray, 'sul', $html, $manager);
	}
	
	private function areaToHtml ($coordsArray, $dir, $html, $manager) {
		if ($dir == 'actual') $div_template = 'area_actual_div'; else $div_template = 'area_move_div';
		$tarea = $this->getArea($this->getCoords($coordsArray, $dir), $manager);
		if ($tarea) {
			$move_dir['move_dir'] = $dir;
			$html->set($div_template, $html->mixArray($move_dir, $div_template));
			$html->set($div_template, $html->mixArray($tarea->toArray(), $div_template));
			$tarea->areaObjsListHtml($html);
			$html->set($div_template, $html->mixArray($tarea->toArray(), $div_template));
			return $html->get($div_template);
		} else return $html->get('area_div');
	}
	
	public function getCoords($coordsArray, $dir) {
		$linha = $coordsArray['y'];
		$coluna = $coordsArray['x'];
		switch($dir) {
			case 'actual': break;
			case 'norte':
				if ($linha - 1 > 0) { $linha--; }
				else return false;
				break;
			case 'sul':
				if ($linha + 1 < 50) { $linha++; }
				else return false;
				break;
			case 'este':
				if ($coluna + 1 < 50) { $coluna++; }
				else return false;
				break;
			case 'oeste':
				if ($coluna - 1 > 0) { $coluna--; }
				else return false;
				break;
			case 'noroeste':
				if ($linha - 1 > 0 && $coluna - 1 > 0) { $linha--; $coluna--; }
				else return false;
				break;
			case 'nordeste':
				if ($linha - 1 > 0 && $coluna + 1 > 0) { $linha--; $coluna++; }
				else return false;
				break;
			case 'sudoeste':
				if ($linha + 1 > 0 && $coluna - 1 > 0) { $linha++; $coluna--; }
				else return false;
				break;
			case 'sudeste':
				if ($linha + 1 > 0 && $coluna + 1 > 0) { $linha++; $coluna++; }
				else return false;
				break;
			default: ;
		}
		
		$array['x'] = $coluna;
		$array['y'] = $linha;
		return $array;
	}
	
	public function getValue($key) {
		return $this->data[$key];
	}
	
	public function toArray() {
		return $this->data;
	}
}
?>